package net.zjitc.service.impl;

import net.zjitc.mapper.AdminMapper;
import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) {
        Admin admin = adminMapper.login(username);
        String new_password = Md5.hashPassword(password,admin.getSalt());
        if(admin.getPassword().equals(new_password)){
            return admin;
        }
        return null;
    }

    @Override
    public Admin ComputerLogin(String username, String password) {
        Admin admin = adminMapper.ComputerLogin(username);
        String new_password = Md5.hashPassword(password,admin.getSalt());
        if(admin.getPassword().equals(new_password)){
            return admin;
        }
        return null;
    }

    @Override
    public Admin getAdmin() {
        return adminMapper.login("admin");
    }

    @Override
    public void register(Admin admin) {
        adminMapper.register(admin.getUsername(),admin.getPassword(),admin.getSalt());
    }
}